home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / answrbok / 8_5.lha / 8_5 / scanset1.c < prev    next >
Text File  |  1993-08-08  |  1KB  |  54 lines

  1. * Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
  2. * The C++ Answer Book */
  3. * Tony Hansen */
  4. * All rights reserved. */
  5. / construct the scanset
  6. canset::scanset(const char *pattern, const char **retpat)
  7.  
  8.    // Set the value which indicates a match based
  9.    // on whether the set is complemented or not.
  10.    int match = 1;
  11.    if (*pattern == '^')
  12. {
  13. match = 0;
  14. pattern++;
  15. }
  16.  
  17.    // Initialize the array for all non-matches.
  18.    memset(set, !match, UCHAR_MAX + 1);
  19.  
  20.    // The first character is special and
  21.    // can contain `]' or `-'.
  22.    if ((*pattern == ']') || (*pattern == '-'))
  23. set[*pattern++] = match;
  24.  
  25.    // Run down the pattern looking for the `]',
  26.    // setting appropriate members of the
  27.    // set to match.
  28.    for ( ; *pattern && (*pattern != ']'); pattern++)
  29. // either a range or trailing `-'
  30. if (*pattern == '-')
  31.     {
  32.     // special case for trailing `-'
  33.     if (pattern[1] == ']')
  34.     set['-'] = match;
  35.  
  36.     // char range, a-z
  37.     else
  38.     {
  39.     for (int i = pattern[-1] + 1;
  40.          i <= pattern[1]; i++)
  41.         set[i] = match;
  42.     pattern++;
  43.     }
  44.     }
  45.  
  46. else
  47.     set[*pattern] = match;
  48.  
  49.    // The pattern now points to the `]'.
  50.    // Return the value just beyond it.
  51.    if (retpat)
  52. *retpat = pattern + 1;
  53.  
  54.